<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];
	/**
	 *
	 * 重写处理没有登录异常跳转方法
	 *
	 * @param \Illuminate\Http\Request $request
	 * @param AuthenticationException $exception
	 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
	 */
	protected function unauthenticated($request, AuthenticationException $exception)
	{
		//获取当前守卫的值
		$guards = $exception->guards();
		//通过判断$guards的值里面是否有一个叫houtai的值,如果有,代表想跳转去后台登录,如果没有,就代表跳转去前台登录
		if (in_array('admin',$guards)){
			$url = '/admin/login';
		}else{
			$url = '/login';
		}
		if ($request->expectsJson()){
			//如果是异步请求,进这个if
			return response()->json(['message' => $exception->getMessage()], 401);
		}else{
			//如果不是异步请求,就进入else
			return redirect()->guest($url);
		}
	}

	/**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}
